package com.unswift.window.extend;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JTextField;

import com.unswift.annotation.api.Api;
import com.unswift.annotation.api.ApiField;

@SuppressWarnings("serial")
@Api(value="带placeholder的文本输入框", author="unswift", date="2023-06-21", version="1.0.0")
public class InputText extends JTextField{

	private static final Color placeholderColor=new Color(180, 180, 180);
	
	@ApiField("提示输入语")
	private String placeholder;
	
	public void setPlaceholder(String placeholder) {
		this.placeholder = placeholder;
	}
	
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		if (placeholder == null || placeholder.length() == 0 || getText().length() > 0) {
			return;
		}
		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		g.setColor(placeholderColor);
		g.drawString(placeholder, getInsets().left, g.getFontMetrics().getMaxAscent() + getInsets().top+3);
	}
	
}
